package leading;

public class FormatMoney {
	public static void main(String[] args) {
	//	getChnMoney(12.02);
	//	formatStr("1234567");
		
	//	System.out.println(getInt("123.03"));
	//	System.out.println(getInt("123.00"));
	//	System.out.println(getInt("123.73"));
	//	String[] strMm = getMoneyValue("123.11");
		System.out.println(getChMoney("15116117111.80"));
		
	}
	
	public static String getChMoney(String numMoney){
		String intMoney = "";
		String dicMoney = "";
		
		String formatIntMoney = "";//转成12位时的字符
		
		String[] gMoneyValue = getMoneyValue(numMoney);
		String gIntMoney = gMoneyValue[0];
		String gDicMoney = gMoneyValue[1];
		//如果传过一00.00则直接返回 -零元整
		if(gIntMoney.equals("00") && gDicMoney.equals("00")){
			return "零元整";
		}
		
		//如果整数部分不为0则将其转成12位的，如果为0则不用转只有小数位了
		if(!gIntMoney.equals("00")&&!gIntMoney.equals("0")){
			formatIntMoney = formatStr(gIntMoney);
			intMoney = getChnMoney(formatIntMoney)+"块";//得到了整数的钱数
		}
		
		if(gDicMoney != null&&Integer.valueOf(gDicMoney)!=0){
			dicMoney = getDicMoney(gDicMoney);
		}
		
		return intMoney + dicMoney;
	}
	
	//得小数后面的钱数
	public static String getDicMoney(String dicMoney){
		String dicc1 = "";
		String dicc2 = "";
		String dic1 = dicMoney.substring(0,1);
		String dic2 = dicMoney.substring(1);
		if(dic1.equals("0")){
			dicc1 = "零";
		}else{
			dicc1 = getNum(dic1)+"毛";
		}
		
		if(!dic1.equals("0")&&!dic2.equals("0")){
			dicc2 = getNum(dic2);
		}else if(dic1.equals("0")&&!dic2.equals("0")){
			dicc2 = getNum(dic2)+"分";
		}
		
		return dicc1 + dicc2;
	}
	
	//将传过来的转成千百十等中文钱数
	public static String getChnMoney(String chnMoney){
		String s1 = chnMoney.substring(0,4);//亿
		String s2 = chnMoney.substring(4,8);//万
		String s3 = chnMoney.substring(8,12);//千
		if(addChn(s1)!=""){
			s1 = addChn(s1) + "亿";
		}else{
			s1 = "";
		}
		if(addChn(s2)!=""){
			
			s2 = addChn(s2) + "万";
		}else{
			s2 = "";
		}
			
		s3 = addChn(s3);
		
		return s1 + s2 + s3;
	}
	
	//三千二百六十二
	public static String addChn(String subMoney){
		String reMoney = "";
		String ss1 = "";
		String ss2 = "";
		String ss3 = "";
		String ss4 = "";
		
		String su1 = subMoney.substring(0,1);//千
		String su2 = subMoney.substring(1,2);//百
		String su3 = subMoney.substring(2,3);//十
		String su4 = subMoney.substring(3);
		//千位
		if(!su1.equals("0")){
			ss1 = getNum(su1) + "千";
		}else{
			ss1 = "";
		}
		//百位
		if(!su2.equals("0")){
			ss2 = getNum(su2) + "百";
		}else if(!su1.equals("0")&&su2.equals("0")){
			ss2 = "零";
		}
		//十倍，如果su2、su3都等于0,则只要加一个零就可以了,su3就不须要加零了
		if(!su3.equals("0")){
			ss3 = getNum(su3) + "十";
		}else if(su1.equals("0")&&su2.equals("0")&&su3.equals("1")){
			ss3 = "十";
		}else if(su2.equals("0")&&su3.equals("0")){
			ss3 = "";
		}else if(!su2.equals("0")&&su3.equals("0")&&!su4.equals("0")){
			ss3 = "零";
		}
		//个位
		if(!su4.equals("0")){
			ss4 = getNum(su4);
		}else{
			ss4 = "";
		}
		if(ss1==""&&ss2==""&&ss3==""&&ss4==""){
			return "";
		}
		return ss1 + ss2 + ss3 + ss4;
	}
	
	//返回一个数组，数组中第一个值是整数部分，第二个值 是小数部分
	public static String[] getMoneyValue(String money){
		String[] moneyValue = new String[2];
		if(!money.contains(".")){
			moneyValue[0] = money;
			return moneyValue;
		}
		
		String[] splitMoney = money.split("[.]");
		moneyValue[0] = splitMoney[0];
		moneyValue[1] = splitMoney[1];
		
		return moneyValue;
	}
	
	
	//将整数部分弄成12位的--千亿最大
	private static String formatStr(String sIn) {
		int n = sIn.length();
		String sOut = sIn;
		// int i = n % 4;

		for (int k = 1; k <= 12 - n; k++) {
			sOut = "0" + sOut;
		}
		System.out.println(sOut);
		return sOut;
	}
	//对应的相应的中文
	private static String getNum(String value) {
		String sNum = "";
		int iValue = Integer.valueOf(value);
		switch (iValue) {
		case 0:sNum = "零";break;
		case 1:sNum = "壹";break;
		case 2:sNum = "贰";break;
		case 3:sNum = "叁";break;
		case 4:sNum = "肆";break;
		case 5:sNum = "伍";break;
		case 6:sNum = "陆";break;
		case 7:sNum = "柒";break;
		case 8:sNum = "捌";break;
		case 9:sNum = "玖";break;
		}
		return sNum;
	}
	
	
	/**
	 * 如果一个字符串数字中小数点后全为零，则去掉小数点及零
	 * 
	 * @param Value
	 *            String
	 * @return String
	 */
	public static String getInt(String Value) {
		if (Value == null) {
			return null;
		}
		String result = "";
	//	boolean mflag = true;
		int m = 0;
		m = Value.lastIndexOf(".");
		if (m == -1) {
			result = Value;
		} else {
			for (int i = m + 1; i <= Value.length() - 1; i++) {
				if (Value.charAt(i) != '0') {
					result = Value;
					return result;	//觉得没必要设一个flag,当发现有一个不等于0直接返回value
				//	mflag = false;
				//	break;
				}
			}
		//	if (mflag) {
		//		result = Value.substring(0, m);
		//	}
		}
		result = Value.substring(0, m);//如果发现的全是0那么截
		
		return result;
	}
	
}	
